package com.ruoyi.mall.service;

import com.ruoyi.health.api.enums.ReportEnum;
import com.ruoyi.mall.api.domian.RecommendProductsVO;
import com.ruoyi.mall.api.domian.Product;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class RecommendProductsStrategy<T> {

    private final  Map<ReportEnum,RecommendProductsService<T>> serviceMap=new ConcurrentHashMap<>();
    public RecommendProductsStrategy(List<RecommendProductsService<T>> list){
        list.forEach(service->{
            serviceMap.put(service.reportType(),service);
        });
    }
    public List<Product> getRecommendProducts(RecommendProductsVO<T> vo){
        return Optional.ofNullable(serviceMap.get(vo.getReportType()))
                .map(service -> service.getRecommendProducts(vo.getParam()))
                .orElse(Collections.emptyList());
    }
}
